package ActorChat

import akka.actor.{Actor, ActorRef, ActorSystem, Props}






case class startMsg(msg:String)
case class endMsg(msg:String)
case class chatMsg(msg:String)



object ActorChat extends App {
  private val myfactory =ActorSystem("myfactory")

  val actorA = myfactory.actorOf(Props[AActor],"A")
  val actorB = myfactory.actorOf(Props[BActor],"B")
  println("AActor 出招了")
  println("start,OK!")
  println("我打")
  actorB ! startMsg("我打")

  class AActor extends Actor{
    var count = 0
    override def receive: Receive ={
      case startMsg(msg) =>{
        count +=1
        println(s"AActor(黄飞鸿)：厉害，看我佛山无影脚...第$count 脚")
        actorB ! chatMsg(s"厉害，看我佛山无影脚...第$count 脚")
      }
      case chatMsg(msg) =>{
        if(count>=3){
          println("AActor(黄飞鸿)：不打了")
          actorB ! endMsg("不打了")

        }else{
          count+=1
          println(s"AActor(黄飞鸿)：厉害，看我佛山无影脚...第${count} 脚")
          actorB ! chatMsg(s"厉害，看我佛山无影脚...第${count} 脚")
        }
      }
      case endMsg(msg)=>{
        println("AActor(黄飞鸿)：后会有期")
        actorB ! endMsg("后会有期")
        context.stop(self)
        //context.system.terminate()
      }
    }
  }

  class BActor extends Actor{
    var count = 0
    override def receive: Receive = {
      case startMsg(msg) =>{
        count +=1
        println(s"BActor(乔峰)：挺猛，看我降龙十八掌...第$count 掌")
        actorA ! chatMsg(s"挺猛，看我降龙十八掌...第${count}掌")
      }
      case chatMsg(msg) =>{
        if(count>=3){
          println("BActor(乔峰)：不打了")
          actorA ! endMsg("不打了")
        }else{
          count+=1
          println(s"BActor(乔峰)：挺猛，看我降龙十八掌...第${count} 掌")
          actorA ! chatMsg(s"挺猛，看我降龙十八掌...第${count}掌")
        }
      }
      case endMsg(msg)=>{
        println("BActor(乔峰)：后会有期")
        actorA ! endMsg("后会有期")
        context.stop(self)
        context.system.terminate()
      }
    }
  }
}
